//  
//  Sprawdzacz
//  
//  Author:
//       Grzegorz Żur <grzegorz.zur@gmail.com>
// 
//  Copyright (c) 2010-2011 Grzegorz Żur
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sprawdzacz
{

	public class Sprawdzacz
	{

		private const int MAX_TEXT_LENGTH = 40;
		
		private const int TIME = 5;
	
		private const TextDataFormat FORMAT = TextDataFormat.Text;
		
		private NotifyIcon notifyIcon = new NotifyIcon();
		
		public Sprawdzacz() {
			Assembly assembly = Assembly.GetExecutingAssembly();
			Stream stream = assembly.GetManifestResourceStream(typeof(Sprawdzacz), "Icon.ico");
			Icon icon = new Icon(stream);
			notifyIcon.Icon = icon;
			notifyIcon.Text = "Walidator numerów używanych w polskiej administracji.";
			ContextMenu menu = new ContextMenu();
			MenuItem help = new MenuItem("Pomoc");
			help.Click += HelpClick;
			menu.MenuItems.Add(help);
			MenuItem exit = new MenuItem("Zakończ");
			exit.Click += ExitClick;
			menu.MenuItems.Add(exit);
			notifyIcon.MouseClick += CheckMouseClick;
			notifyIcon.DoubleClick += CheckClick;
			notifyIcon.ContextMenu = menu;
			notifyIcon.Visible = true;
		}

		private void CheckMouseClick(object sender, MouseEventArgs args)	{
			if (args.Button == MouseButtons.Left) {
				Check();
			}
		}
		
		private void CheckClick(object sender, EventArgs args)	{
			Check();
		}
		
		private void HelpClick(object sender, EventArgs args)	{
			Help();
		}
		
		private void ExitClick(object sender, EventArgs args) {
			Exit();
		}
		
		private void Show(ToolTipIcon icon, string title, string text) {
			notifyIcon.BalloonTipIcon = icon;
			notifyIcon.BalloonTipTitle = title;
			notifyIcon.BalloonTipText = text;
			notifyIcon.ShowBalloonTip(TIME);
		}
		
		private void Check() {
			if (!Clipboard.ContainsText(FORMAT)) {
				Show(ToolTipIcon.Error, "Nieprawidłowe dane w schowku", "W schowku powinien znajdować się zwykły tekst");
				return;
			}
			string text = Clipboard.GetText(FORMAT);
			if (text == null || text.Trim().Equals("")) {
				Show(ToolTipIcon.Error, "Brak numeru w schowku", "Najpierw skopiuj numer do schowka a potem kliknij");
				return;
			}
			Validate(text);
		}
		
		private void Help() {
			string message = "Skopiuj numer do schowka a potem kliknij w ikonę tego programu w zasobniku.\n\nRozpoznawane numery:\n";
			foreach (Validator validator in Validators.VALIDATORS)
				message += validator.Name + "\n";
			Show(ToolTipIcon.Info, "Pomoc", message);
		}
		
		private void Validate(string text) {
			Validation[] validations = Validators.Validate(text);
			if (validations.Length == 0) {
				string message = text.Length <= MAX_TEXT_LENGTH ? text : (text.Substring(0, MAX_TEXT_LENGTH) + "...");	
				Show(ToolTipIcon.Error, "Niepoprawny numer", message);
			} else if (validations.Length == 1) {
				Validation validation = validations[0];
				Show(ToolTipIcon.Info, validation.Name, validation.Message);
			} else {
				string message = String.Empty;
				foreach (Validation validation in validations)
					message += String.Format("{0} {1}\n", validation.Name, validation.Message);
				Show(ToolTipIcon.Info, "Wiele dopasowań", message);
			}
		}
		
		private void Exit()	{
			notifyIcon.Visible = false;
			notifyIcon.Dispose();
			Application.Exit();
		}
		
		[STAThreadAttribute]
		public static void Main() {
			new Sprawdzacz();
			Application.Run();
		}
		
	}
	
}
